package mysql

import (
	"gorm.io/gorm"
)

type Institution struct {
	gorm.Model
	DoctorTeamId        int    `gorm:"type:int(10);not null;comment:'医生团队Id'"`
	InstitutionName     string `gorm:"type:varchar(100);not null;comment:'机构名称'"`
	InstitutionInternal string `gorm:"type:varchar(255);not null;comment:'机构简介'"`
	InstitutionPhone    string `gorm:"type:varchar(11);not null;comment:'机构手机号'"`
	InstitutionImage    string `gorm:"type:varchar(255);not null;comment:'机构封面'"`
}

func GetInstitutionList(pages int, size int, InstitutionName string) (result []*Institution, err error) {
	page := pages
	if page <= 0 {
		page = 1
	}

	pageSize := size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	tx := DB.Debug()
	if InstitutionName != "" {
		tx = tx.Where("institution_name LIKE ?", "%"+InstitutionName+"%")
	}
	err = tx.Offset(offset).Limit(pageSize).Find(&result).Error
	if err != nil {
		return nil, err
	}
	return
}
